/* $Id: ClientRoleReqDAOImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.dao.provider;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries;
import org.springframework.stereotype.Repository;

import ae.pcfc.etks.online.common.ConstantsCommon;
import ae.pcfc.etks.online.common.ConstantsCommon.RequestType;
import ae.pcfc.etks.online.common.dao.ClientRoleReqDAO;
import ae.pcfc.etks.online.common.domain.AppMetaData;
import ae.pcfc.etks.online.common.domain.ClientRoleReq;

@Repository
public class ClientRoleReqDAOImpl extends BaseDAO implements ClientRoleReqDAO {

	/**
	 * add
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientRoleReqDAO#add(ae.pcfc.etks.online.common.domain.ClientRoleReq)
	 * 
	 */
	public void add(ClientRoleReq clientRoleReq) {

		getHibernateTemplate().getSessionFactory().getCurrentSession().save(clientRoleReq);

	}

	/**
	 * findClientRoleReqByApplicationId
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientRoleReqDAO#findClientRoleReqByApplicationId(java.lang.String)
	 * 
	 */
	public List<ClientRoleReq> findClientRoleReqByApplicationId(String applicationId) {

		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createCriteria(ClientRoleReq.class, "roleReq");
		criteria.add(Restrictions.eq("roleReq.applicationId", applicationId));

		@SuppressWarnings("unchecked")
		List<ClientRoleReq> clientRoleReqs = criteria.list();

		return (clientRoleReqs != null && clientRoleReqs.size() > 0) ? clientRoleReqs : null;

	}

	/**
	 * findClientRoleReqByClientAndRoleId
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientRoleReqDAO#findClientRoleReqByClientAndRoleId(java.lang.Long, java.lang.Long)
	 * 
	 */
	public ClientRoleReq findClientRoleReqByClientAndRoleId(Long clientId, Long roleId) {

		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createCriteria(ClientRoleReq.class, "roleReq");
		criteria.add(Restrictions.eq("roleReq.roleId", roleId));
		criteria.createAlias("roleReq.client", "client");
		criteria.add(Restrictions.eq("client.CId", clientId));

		/************** Sub Criteria for the roles which are already requested and rejected *****************/
		DetachedCriteria alreadyRequsted = DetachedCriteria.forClass(AppMetaData.class, "appMetaData");
		alreadyRequsted.setProjection(Projections.property("appMetaData.applicationId"));
		alreadyRequsted.add(Restrictions.like("appMetaData.clientRefNo", RequestType.ROLE.getCode()+"%"));
		alreadyRequsted.add(Restrictions.ne("appMetaData.wfStatus", ConstantsCommon.REJECT));
		
		
		// Setting for the roles which are already registered and rejected exclude them
		criteria.add(Subqueries.propertyIn("roleReq.applicationId", alreadyRequsted));
		@SuppressWarnings("unchecked")
		List<ClientRoleReq> clientRoleReqs = criteria.list();

		return (clientRoleReqs != null && clientRoleReqs.size() > 0) ? clientRoleReqs.get(0) : null;
	}

	/**
	 * findClientRoleReqByTrakheesId
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientRoleReqDAO#findClientRoleReqByTrakheesId(java.lang.String)
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<ClientRoleReq> findClientRoleReqByTrakheesId(String trakheesId) {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createCriteria(ClientRoleReq.class, "roleReq");
		criteria.createAlias("roleReq.client", "client");
		criteria.add(Restrictions.eq("client.CTrakheesId", trakheesId));

		/************** Sub Criteria for the roles which are already requested and rejected *****************/
		DetachedCriteria alreadyRequsted = DetachedCriteria.forClass(AppMetaData.class, "appMetaData");
		alreadyRequsted.setProjection(Projections.property("appMetaData.applicationId"));
		alreadyRequsted.add(Restrictions.like("appMetaData.clientRefNo", RequestType.ROLE.getCode()+"%"));
		alreadyRequsted.add(Restrictions.ne("appMetaData.wfStatus", ConstantsCommon.REJECT));
		
		
		// Setting for the roles which are already registered and rejected exclude them
		criteria.add(Subqueries.propertyIn("roleReq.applicationId", alreadyRequsted));


		return (criteria.list() != null && criteria.list().size() > 0) ? criteria.list() : null;

	}

}
